--
-- Author: yawei.zhu
-- Date: 2018-05-02 11:19:47
--
-- TaskView
--

local ListMenu = require("app.widget.ListMenu")
local FirstTaskTargetRender = import(".renders.FirstTaskTargetRender")
local SecondTaskTargetRender = import(".renders.SecondTaskTargetRender")

local MainTaskPanel = import(".MainTaskPanel")
local DailyTaskPanel = import(".DailyTaskPanel")
local GoldTaskPanel = import(".GoldTaskPanel")
local BranchTaskPanel = import(".BranchTaskPanel")
local WeeklyTaskPanel = import(".WeeklyTaskPanel")
local RoadHeroPanel = import(".RoadHeroPanel")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)
ClassRef.VIEW_ROOT_RES = Res.TaskUiScene

function ClassRef:init()
	self._ui.Button_back:addClickEventListener(function()
		executeFunc(self._onCloseView)
	end)

	local function operate(op, data)
		if op == "btnClick" then
			executeFunc(self._onOperate, "selectedTarget", data)
		elseif op == "subBtnClick" then
			executeFunc(self._onOperate, "selectedSubTarget", data)
		end
	end

	local params = {
		list = self._ui["Panel/ListView"],
		btnClass = FirstTaskTargetRender,
		subBtnClass = SecondTaskTargetRender,
		operate = operate,
		mustHaveTarget = true,
		onlyOpen = true,
        autoHideSecond = true,
	}
	self._menuList = ListMenu.new(params)

	self._detailRoot = self._ui["Panel/mainline"]
    self._detailRoot:removeAllChildren()

    self._bindPanel = {}
end

-- 检查是否触发了引导
function ClassRef:checkGuide()
    local params = {
        scene = self._funcId,
        sceneSelf = self,
        getFunc = self.getGuideNodeFunc,
        nextFunc = self.checkGuide
    }
    EM:notify("checkGuide", params)
end

-- 获取引导node
function ClassRef:getGuideNodeFunc(id)
    if id == 143 then
        -- 143 奖励提示
        local index = executeFunc(self._onOperate, "getMenuDataByFuncId", Constant.FUNC_TYPE_TASK_DAILY).index
        return self._bindPanel[Constant.FUNC_TYPE_TASK_DAILY]._ui["Panel/Panel_top/gift_1/Panel"]
    elseif id == 144 then
        -- 144 返回赏金任务按钮
        local index = executeFunc(self._onOperate, "getMenuDataByFuncId", Constant.FUNC_TYPE_TASK_GOLD).index
        return self._menuList:getMenuItem(index)
    end
end


function ClassRef:refreshMenuList(menuData)
    self._menuData = menuData
	self._menuList:refreshList(menuData)

    -- 设置红点
    -- local dataList = self._onOperate("getMenuList")
    for i, data in ipairs(menuData) do
        local isShow = TaskManager:checkHasRewardByTaskType(data.type)
        self._menuList:setBtnRedPoint(i, 0, isShow)

        -- 赏金任务去设置二级按钮
        if data.type == 4 then
            for j, subData in ipairs(data.subDatas) do
                if not isShow then 
                    self._menuList:setBtnRedPoint(i, j, false)
                else
                    local isSubShow = false
                    local tasks = self._onOperate("getBountyTasks")[tonumber(subData.ID)]
                    for _, task in ipairs(tasks) do
                        if task:isFinished() then
                            isSubShow = true
                            break
                        end
                    end
                    self._menuList:setBtnRedPoint(i, j, isSubShow)
                end
                
            end
        end
    end
end

function ClassRef:setTargetSelected( firstTarget, secondTarget )
	firstTarget = firstTarget or 1
	secondTarget = secondTarget or 1
	self._menuList:selectBtn( firstTarget, secondTarget )
end

function ClassRef:showTaskView(op, data)
	if op == "selectedTarget" then

		for _, panel in pairs(self._bindPanel) do
            panel:setVisible(false)
        end

        if data.id ~= Constant.FUNC_TYPE_TASK and 
            data.id ~= Constant.FUNC_TYPE_TASK_DAILY and 
            data.id ~= Constant.FUNC_TYPE_TASK_GOLD and
            data.id ~= Constant.FUNC_TYPE_TASK_BRANCH and 
            data.id ~= Constant.FUNC_TYPE_TASK_WEEKLY and
            data.id ~= Constant.FUNC_TYPE_TASK_ROADHERO then
            return
        end

        self:showPanel(data.id)
        self._funcId = data.id
        self:checkGuide()
		
	elseif op == "selectedSubTarget" then
        local panel = self._bindPanel[Constant.FUNC_TYPE_TASK_GOLD]
        if panel then panel:updateCircleTask(data.ID) end
	end
end

function ClassRef:showPanel(funcId)
    if not self._bindPanel[funcId] then
        local panel 
        if funcId == Constant.FUNC_TYPE_TASK then
            panel = MainTaskPanel.create()
        elseif funcId == Constant.FUNC_TYPE_TASK_DAILY then
            panel = DailyTaskPanel.create()
        elseif funcId == Constant.FUNC_TYPE_TASK_GOLD then
            panel = GoldTaskPanel.create()
        elseif funcId == Constant.FUNC_TYPE_TASK_BRANCH then
            panel = BranchTaskPanel.create()
        elseif funcId == Constant.FUNC_TYPE_TASK_WEEKLY then
            panel = WeeklyTaskPanel.create()
        elseif funcId == Constant.FUNC_TYPE_TASK_ROADHERO then
            panel = RoadHeroPanel.create()
        end

        if panel.onOperate then
            panel:onOperate(self._onOperate)
        end
        panel:addTo(self._detailRoot)
        panel:refresh()
        self._bindPanel[funcId] = panel
    end

    self._bindPanel[funcId]:setVisible(true)

    -- 需要额外处理的
    if funcId == Constant.FUNC_TYPE_TASK_DAILY then
        executeFunc(self._onOperate, "GET_DAILY_INFO")
    elseif funcId == Constant.FUNC_TYPE_TASK_WEEKLY then
        executeFunc(self._onOperate, "GET_WEEKLY_INFO")
    elseif funcId == Constant.FUNC_TYPE_TASK_GOLD then
        executeFunc(self._onOperate, "GET_BOUNTY_TASK_INFO")
    elseif funcId == Constant.FUNC_TYPE_TASK_ROADHERO then
        executeFunc(self._onOperate, "reqRoadOfHeroProcess")
    end
end

-- 更新英雄之路信息
function ClassRef:updateRoadHeroInfo(process)
    local panel = self._bindPanel[Constant.FUNC_TYPE_TASK_ROADHERO]
    if panel and panel:isVisible() then 
        panel:updateRoadHeroInfo(process)
    end

    -- 更新红点
    self:updateRedPointByFuncId(Constant.FUNC_TYPE_TASK_ROADHERO)
end

function ClassRef:updateTaskProgress(taskType)
    if taskType == "daily" then
        local panel = self._bindPanel[Constant.FUNC_TYPE_TASK_DAILY]
        if panel then panel:updateDailyTaskProgress() end
        -- 更新红点
        self:updateRedPointByFuncId(Constant.FUNC_TYPE_TASK_DAILY)
    elseif taskType == "weekly" then
        local panel = self._bindPanel[Constant.FUNC_TYPE_TASK_WEEKLY]
        if panel then panel:updateWeeklyTaskProgress() end
        -- 更新红点
        self:updateRedPointByFuncId(Constant.FUNC_TYPE_TASK_WEEKLY)
    end
end

-- 日常
function ClassRef:onGetDailyTaskAward(taskId)
    local panel = self._bindPanel[Constant.FUNC_TYPE_TASK_DAILY]
    if panel then panel:updateDailyTasksShow(taskId) end

    -- 更新红点
    self:updateRedPointByFuncId(Constant.FUNC_TYPE_TASK_DAILY)
end

function ClassRef:refreshDailyTask(pos)
    local panel = self._bindPanel[Constant.FUNC_TYPE_TASK_DAILY]
    if panel then panel:refreshDailyTask(pos) end
end

function ClassRef:playDailyRefreshAni(pos)
    local panel = self._bindPanel[Constant.FUNC_TYPE_TASK_DAILY]
    if panel then panel:playDailyRefreshAni(pos) end
end

-- 每周
function ClassRef:onGetWeeklyTaskAward(taskId)
    local panel = self._bindPanel[Constant.FUNC_TYPE_TASK_WEEKLY]
    if panel then panel:updateWeeklyTasksShow(taskId) end

    -- 更新红点
    self:updateRedPointByFuncId(Constant.FUNC_TYPE_TASK_WEEKLY)
end


-- 赏金
function ClassRef:updateCircleTasks(fameSubId)
    local panel = self._bindPanel[Constant.FUNC_TYPE_TASK_GOLD]
    if panel then panel:updateCircleTask(fameSubId) end
end

function ClassRef:playRefreshAni(index)
    local panel = self._bindPanel[Constant.FUNC_TYPE_TASK_GOLD]
    if panel then panel:playRefreshAni(index) end
end

function ClassRef:refreshCircleTask()
    -- 更新红点
    self:updateRedPointByFuncId(Constant.FUNC_TYPE_TASK_GOLD)

    -- 更新二级按钮红点
    local data = self._onOperate("getMenuDataByFuncId", Constant.FUNC_TYPE_TASK_GOLD)
    local isShow = TaskManager:checkHasRewardByTaskType(4)
    for j, subData in ipairs(data.subDatas) do
        if not isShow then 
            self._menuList:setBtnRedPoint(data.index, j, false)
        else
            isShow = false
            local tasks = self._onOperate("getBountyTasks")[tonumber(subData.ID)]
            for _, task in ipairs(tasks) do
                if task:isFinished() then
                    isShow = true
                    break
                end
            end
            self._menuList:setBtnRedPoint(data.index, j, isShow)
        end
        
    end
    
end

-- 支线
function ClassRef:updateBranchTrackTask(taskId)
    local panel = self._bindPanel[Constant.FUNC_TYPE_TASK_BRANCH]
    if panel then panel:updateBranchTrackTask(taskId) end
    
end

function ClassRef:onGetBranchTaskAward(taskId)
    local panel = self._bindPanel[Constant.FUNC_TYPE_TASK_BRANCH]
    if panel then panel:getBranchTaskBack(taskId) end

    -- 更新红点
    self:updateRedPointByFuncId(Constant.FUNC_TYPE_TASK_BRANCH)
end

-- 更新红点
function ClassRef:updateRedPointByFuncId(id)
    local data = executeFunc(self._onOperate, "getMenuDataByFuncId", id)
    if not data then return end

    local isShow = TaskManager:checkHasRewardByTaskType(data.type)
    self._menuList:setBtnRedPoint(data.index, 0, isShow)
end

-- 更新最新任务
function ClassRef:updateNewTasks(tasks)
    local panel = self._bindPanel[self._funcId]
    if panel and panel.updateNewTasks then panel:updateNewTasks(tasks) end
end

return ClassRef
